﻿#include "MirMessageBox.h"
#include "../MirGraphics/MLibrary.h"
#include "../Settings.h"
#include "MirScene.h"
#include "../Program.h"
#include "MirTextBox.h"
#include "../MirScenes/GameScene.h"

using namespace Client::MirGraphics;
using namespace Client::MirScenes;

namespace Client::MirControls
{

    MirMessageBox::MirMessageBox(const std::string &message, MirMessageBoxButtons b, bool allowKeys)
    {
        setDrawImage(true);
        setForeColour(Color::White);
        Buttons = b;
        setModal(true);
        setMovable(false);
        AllowKeyPress = allowKeys;

        setIndex(360);
        setLibrary(Libraries::Prguse);

        Point tempVar((Settings::ScreenWidth - getSize()->Width) / 2, (Settings::ScreenHeight - getSize()->Height) / 2);
        setLocation(&tempVar);


        Label = new MirLabel();
        Label->setAutoSize(false);
        Point tempVar2(35, 35);
        Label->setLocation(&tempVar2);
        Size tempVar3(390, 110);
        Label->setSize(&tempVar3);
        Label->setParent(this);
        Label->setText(message);


        switch (Buttons)
        {
            case MirMessageBoxButtons::OK:
                OKButton = new MirButton();
                OKButton->setHoverIndex(201);
                OKButton->setIndex(200);
                OKButton->setLibrary(Libraries::Title);
                Point tempVar4(360, 157);
                OKButton->setLocation(&tempVar4);
                OKButton->setParent(this);
                OKButton->setPressedIndex(202);
                OKButton->Click += [&] (o, e)
                {
                    Dispose();
                };
                break;
            case MirMessageBoxButtons::OKCancel:
                OKButton = new MirButton();
                OKButton->setHoverIndex(201);
                OKButton->setIndex(200);
                OKButton->setLibrary(Libraries::Title);
                Point tempVar5(260, 157);
                OKButton->setLocation(&tempVar5);
                OKButton->setParent(this);
                OKButton->setPressedIndex(202);
                OKButton->Click += [&] (o, e)
                {
                    Dispose();
                };
                CancelButton = new MirButton();
                CancelButton->setHoverIndex(204);
                CancelButton->setIndex(203);
                CancelButton->setLibrary(Libraries::Title);
                Point tempVar6(360, 157);
                CancelButton->setLocation(&tempVar6);
                CancelButton->setParent(this);
                CancelButton->setPressedIndex(205);
                CancelButton->Click += [&] (o, e)
                {
                    Dispose();
                };
                break;
            case MirMessageBoxButtons::YesNo:
                YesButton = new MirButton();
                YesButton->setHoverIndex(207);
                YesButton->setIndex(206);
                YesButton->setLibrary(Libraries::Title);
                Point tempVar7(260, 157);
                YesButton->setLocation(&tempVar7);
                YesButton->setParent(this);
                YesButton->setPressedIndex(208);
                YesButton->Click += [&] (o, e)
                {
                    Dispose();
                };
                NoButton = new MirButton();
                NoButton->setHoverIndex(211);
                NoButton->setIndex(210);
                NoButton->setLibrary(Libraries::Title);
                Point tempVar8(360, 157);
                NoButton->setLocation(&tempVar8);
                NoButton->setParent(this);
                NoButton->setPressedIndex(212);
                NoButton->Click += [&] (o, e)
                {
                    Dispose();
                };
                break;
            case MirMessageBoxButtons::YesNoCancel:
                YesButton = new MirButton();
                YesButton->setHoverIndex(207);
                YesButton->setIndex(206);
                YesButton->setLibrary(Libraries::Title);
                Point tempVar9(160, 157);
                YesButton->setLocation(&tempVar9);
                YesButton->setParent(this);
                YesButton->setPressedIndex(208);
                YesButton->Click += [&] (o, e)
                {
                    Dispose();
                };
                NoButton = new MirButton();
                NoButton->setHoverIndex(211);
                NoButton->setIndex(210);
                NoButton->setLibrary(Libraries::Title);
                Point tempVar10(260, 157);
                NoButton->setLocation(&tempVar10);
                NoButton->setParent(this);
                NoButton->setPressedIndex(212);
                NoButton->Click += [&] (o, e)
                {
                    Dispose();
                };
                CancelButton = new MirButton();
                CancelButton->setHoverIndex(204);
                CancelButton->setIndex(203);
                CancelButton->setLibrary(Libraries::Title);
                Point tempVar11(360, 157);
                CancelButton->setLocation(&tempVar11);
                CancelButton->setParent(this);
                CancelButton->setPressedIndex(205);
                CancelButton->Click += [&] (o, e)
                {
                    Dispose();
                };
                break;
            case MirMessageBoxButtons::Cancel:
                CancelButton = new MirButton();
                CancelButton->setHoverIndex(204);
                CancelButton->setIndex(203);
                CancelButton->setLibrary(Libraries::Title);
                Point tempVar12(360, 157);
                CancelButton->setLocation(&tempVar12);
                CancelButton->setParent(this);
                CancelButton->setPressedIndex(205);
                CancelButton->Click += [&] (o, e)
                {
                    Dispose();
                };
                break;
        }
    }

    void MirMessageBox::Show()
    {
        if (getParent() != nullptr)
        {
            return;
        }

        setParent(MirScene::ActiveScene);

        Highlight();

        for (int i = 0; i < Program::Form->Controls->Count; i++)
        {
            TextBox *T = dynamic_cast<TextBox*>(Program::Form->Controls[i]);
            if (T != nullptr && T->Tag != nullptr && T->Tag != nullptr)
            {
                (static_cast<MirTextBox*>(T->Tag))->DialogChanged();
            }
        }

        if (dynamic_cast<GameScene*>(MirScene::ActiveScene) != nullptr)
        {
            (static_cast<GameScene*>(getParent()))->BigMapDialog->Hide();
        }
    }

    void MirMessageBox::OnKeyDown(KeyEventArgs *e)
    {
        if (AllowKeyPress)
        {
            MirImageControl::OnKeyDown(e);
            e->Handled = true;
        }
    }

    void MirMessageBox::OnKeyUp(KeyEventArgs *e)
    {
        if (AllowKeyPress)
        {
            MirImageControl::OnKeyUp(e);
            e->Handled = true;
        }
    }

    void MirMessageBox::OnKeyPress(KeyPressEventArgs *e)
    {
        MirImageControl::OnKeyPress(e);

        if (AllowKeyPress)
        {
            if (e->KeyChar == static_cast<char>(Keys->Escape))
            {
                switch (Buttons)
                {
                    case MirMessageBoxButtons::OK:
                        if (OKButton != nullptr && !OKButton->isDisposed())
                        {
                            OKButton->InvokeMouseClick(nullptr);
                        }
                        break;
                    case MirMessageBoxButtons::OKCancel:
                    case MirMessageBoxButtons::YesNoCancel:
                        if (CancelButton != nullptr && !CancelButton->isDisposed())
                        {
                            CancelButton->InvokeMouseClick(nullptr);
                        }
                        break;
                    case MirMessageBoxButtons::YesNo:
                        if (NoButton != nullptr && !NoButton->isDisposed())
                        {
                            NoButton->InvokeMouseClick(nullptr);
                        }
                        break;
                }
            }

            else if (e->KeyChar == static_cast<char>(Keys->Enter))
            {
                switch (Buttons)
                {
                    case MirMessageBoxButtons::OK:
                    case MirMessageBoxButtons::OKCancel:
                        if (OKButton != nullptr && !OKButton->isDisposed())
                        {
                            OKButton->InvokeMouseClick(nullptr);
                        }
                        break;
                    case MirMessageBoxButtons::YesNoCancel:
                    case MirMessageBoxButtons::YesNo:
                        if (YesButton != nullptr && !YesButton->isDisposed())
                        {
                            YesButton->InvokeMouseClick(nullptr);
                        }
                        break;

                }
            }
            e->Handled = true;
        }
    }

    void MirMessageBox::Show(const std::string &message, bool close)
    {
        MirMessageBox *box = new MirMessageBox(message);

        if (close)
        {
            box->OKButton->Click += [&] (o, e)
            {
                Program::Form->Close();
            };
        }

        box->Show();

        delete box;
    }

    void MirMessageBox::Dispose(bool disposing)
    {

        MirImageControl::Dispose(disposing);

        if (!disposing)
        {
            return;
        }

        Label = nullptr;
        OKButton = nullptr;
        CancelButton = nullptr;
        NoButton = nullptr;
        YesButton = nullptr;
        Buttons = (Client::MirControls::MirMessageBoxButtons)0;

        for (int i = 0; i < Program::Form->Controls->Count; i++)
        {
            TextBox *T = static_cast<TextBox*>(Program::Form->Controls[i]);
            if (T != nullptr && T->Tag != nullptr)
            {
                (static_cast<MirTextBox*>(T->Tag))->DialogChanged();
            }
        }
    }
}
